<?php

namespace app\Api\event;

use think\Controller;
use think\Loader;
use think\Db;

class Base extends Controller
{
	/**
	 * [getTimeDiff 获取时间差]
	 * @return [type] [description]
	 */
    public function getTimeDiff()
    {
    	$sdate = $this->request->param('sadte');
    	$edate = $this->request->param('edate') ? $this->request->param('edate'): $sdate;
    	return (strtotime($edate)-strtotime($sdate))/(24*60*60)+1;
    }
    /**
     * [getCampInfo description]
     * @return [type] [description]
     */
    public function getCampInfo()
    {
    	return Db::table('camps')->where('id',$this->request->param('id'))->find();
    }
    public function getOrderPrice()
    {
    	return Loader::model('Orders')->calculatePrice($this->request->param('id'), $this->request->param('sdate'), $this->request->param('edate'))->toArray();
    }
    /**
     * [getContactionInfo 联系人]
     * @return [type] [description]
     */
    public function getContactionInfo()
    {
        return Db::table('contactioninfo')->where('id',$this->request->param('contactid'))->find();
    }
    public function getOrderDetailInfo()
    {
        $status = [
            '01'  => '等待支付',
            '10'  => '继续支付',
            '11'  => '已支付订单',
            '20'  => '订单退款中',
            '21'  => '已退款订单',
            '31'  => '订单完成',
            '41'  => '已取消订单',
            '50'  => '入住中'
        ];
        $orderinfo = Loader::model('Orders')->where('number',$this->request->param('no'))->find();
        //订单类型
        $orderinfo->ordertype = $status[$orderinfo->stage.$orderinfo->status];
        //营地
        $orderinfo->camps = Db::table('camps')->where('id',$orderinfo->camp_id)->find();
        //房车
        $orderinfo->caravaninfo = Db::table('caravan')->where('id',$orderinfo->caravan_id)->find();
        //联系人
        $orderinfo->contactioninfo = Db::table('contactioninfo')->where(['id'=>['in',$orderinfo->rent_contact_id]])->select();
        return $orderinfo;
        
    }
    public function postChangeContactInfo()
    {
       $type = $this->request->param('contype');
       $contactinfo = array_merge(['user_id'=>\app\api\controller\Index::$api_user->id],$this->request->param('contactinfo/a'));
       if ($type == 1) {
          return Db::table('contactioninfo')->insert($contactinfo);
       }else{
          return Db::table('contactioninfo')->where('id',$this->request->param('contactid'))->update($contactinfo);  
       }
    }
    /**
     * [getUserAllContacts 获取用户所有联系人]
     * @return [type] [description]
     */
    public function getUserAllContacts()
    {
        return Db::table('contactioninfo')->where('user_id',\app\api\controller\Index::$api_user->id)->order('created_at','desc')->select();
    }
    public function postCreateOrder()
    {
        $info = array_merge(['rent_user_id'=>\app\api\controller\Index::$api_user->id],$this->request->param('orderinfo/a'));
        return Loader::model('Orders')->createOrders($info);
    }
}